package com.dmcloud.system.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dmcloud.common.mybatis.Condition;
import com.dmcloud.common.mybatis.Query;
import com.dmcloud.common.util.auth.DmUser;
import com.dmcloud.common.util.reponse.ResultMessage;
import com.dmcloud.system.entity.SysUser;
import com.dmcloud.system.service.ISysUserService;
import com.dmcloud.system.vo.UserVO;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Delete;
import org.springframework.web.bind.annotation.*;


/**
 * Created by xiaodao
 * date: 2019/10/30
 * 用户操作
 */
@RestController
@RequestMapping("/user")
@Slf4j
@AllArgsConstructor
public class UserController {

    private ISysUserService sysUserService;

    @Delete("/delete/{id}")
    public ResultMessage delete(@PathVariable String id) {
        sysUserService.removeById(id);
        return ResultMessage.success(id,"删除成功");
    }

    @GetMapping("/getUser/{id}")
    @ResponseBody
    public ResultMessage getUser(@PathVariable String id) {
        return ResultMessage.success(sysUserService.getUserVoById(id));
    }

    @GetMapping("/query")
    public ResultMessage queryUser(Page page, SysUser user) {
        IPage list = sysUserService.getAllUser(page, user);
        return ResultMessage.success(list);
    }

    /**
     * 根据用户名获取用户信息
     *
     * @return
     */
    @GetMapping("/getUserInfo")
    public ResultMessage info(DmUser dmUser) {
        SysUser user = sysUserService.getOne(Wrappers.<SysUser>query().lambda().eq(SysUser::getAccount, dmUser.getAccount()));
        if (null == user) {
            return ResultMessage.failed(String.format("用户名不存在 %s", dmUser.getAccount()));
        }
        return ResultMessage.success(user);
    }

    @GetMapping("/logout")
    public ResultMessage logout() {
        return ResultMessage.success();
    }

    /**
     * 获取用户列表
     *
     * @param query
     * @param userVO
     * @return
     */
    @PostMapping("/getUserList")
    public ResultMessage getUserList(Query query, UserVO userVO) {
        IPage<SysUser> userPage = sysUserService.page(Condition.getPage(query), null);

        return ResultMessage.success(userPage);
    }

    /**
     * 新增或者修改
     *
     * @param sysUser
     * @return
     */
    @PostMapping("/saveUser")
    public ResultMessage saveUser(@RequestBody SysUser sysUser) {
        sysUserService.save(sysUser);
        return ResultMessage.success();
    }


    @GetMapping("/testDmUser")
    public ResultMessage testDmUser(DmUser dmUser){
        System.out.println(dmUser);
        return ResultMessage.success(dmUser);
    }
}
